package com.test31_IO应用;

import com.test31_IO应用.util.InputUtil;

import java.util.Arrays;

class Student implements Comparable<Student> {
    private String name;
    private double score;

    public Student(String name, double score) {
        this.name = name;
        this.score = score;
    }

    @Override
    public int compareTo(Student o) {
        if (this.score > o.score) {
            return 1;
        } else if (this.score < o.score) {
            return -1;
        }
        return 0;
    }

    @Override
    public String toString() {
        return "【Student】姓名：" + this.name + "、成绩：" + this.score;
    }
}

interface IStudentService {
    public Student[] getStudents();
}

class StudentServiceImpl implements IStudentService {
    private static String INPUT_REGEX = "([a-zA-Z]+:\\d+(\\.\\d+)?\\|?)+";
    private Student[] students;

    public StudentServiceImpl() {
        input();    // 键盘输入数据
    }

    /** 处理数据的输入操作 */
    public void input() {
        String value = InputUtil.getString("请输入字符串（Tom:98.2|Jerry:99.6|Tony:68.9）：", INPUT_REGEX);
        String[] result = value.split("\\|");   // 正则拆分
        this.students = new Student[result.length];
        for (int x = 0; x < result.length; x++) {
            String[] temp = result[x].split(":");
            this.students[x] = new Student(temp[0], Double.parseDouble(temp[1]));
        }
        Arrays.sort(this.students);     // 对象数组排序
    }

    @Override
    public Student[] getStudents() {
        return this.students;
    }
}

class StudentFactory {
    private StudentFactory() {
    }

    public static IStudentService getInstance() {
        return new StudentServiceImpl();
    }
}

/**
 * 输入数据排序
 */
public class YootkDemo_03 {

    public static void main(String[] args) {
//        String regex = "([a-zA-Z]+:\\d+(\\.\\d+)?\\|?)+";
//        System.out.println(InputUtil.getString("请输入字符串（Tom:98.2|Jerry:99.6|Tony:68.9）：", regex));
        IStudentService studentService = StudentFactory.getInstance();
        Student[] students = studentService.getStudents();
        Arrays.stream(students).forEach(System.out::println);
    }
}
